package cn.szzsi.common.model;


import cn.szzsi.common.model.base.BaseAdmin;
import com.jfinal.plugin.activerecord.Db;
import org.apache.commons.codec.digest.DigestUtils;

import java.util.List;

/**
 * 实体类 - 管理员
 */
public class Admin extends BaseAdmin<Admin> {

    private static final long serialVersionUID = -57555379613217315L;

    public static final String HASH_ALGORITHM = "SHA-1";
    public static final int HASH_INTERATIONS = 1024;
    public static final String LOGIN_ADMIN_ID="adminId";//超级管理员登陆id

    public static final Admin dao = new Admin().dao();

    /*public List<PlatformRole> getRoleList() {
        String sql = ""
                + " select r.*"
                + "  from mall_admin_role a left outer join mall_role r on a.roleset_id = r.id "
                + " where a.adminset_id = ?";
        return PlatformRole.dao.find(sql, getStr("id"));
    }*/

    /**
     * 根据用户名获取管理员对象，若管理员不存在，则返回null（不区分大小写）
     */
    public Admin getAdminByUsername(String username) {
        return dao.findFirst("select * from mall_admin where username = ?", username);
    }

    /**
     * 根据用户名、密码验证管理员
     *
     * @param username 用户名
     * @param password 密码
     * @return 验证是否通过
     */
    public boolean verifyAdmin(String username, String password) {
        Admin member = getAdminByUsername(username);
        if (member != null && member.getStr("password").equals(DigestUtils.md5Hex(password))) {
            return true;
        } else {
            return false;
        }
    }


    /**
     * 根据用户ID查询该用户所拥有的角色列表
     *
     * @return
     */
    public List<String> getRolesName(String adminId) {
        String sql = ""
                + "select r.value"
                + "  from mall_admin a"
                + "  left outer join mall_admin_role ar"
                + "    on a.id = ar.adminset_id"
                + "  left outer join mall_role r"
                + "    on r.id = ar.roleset_id"
                + " where a.id = ?";

        return Db.query(sql, adminId);
    }

    /**
     * 根据用户ID查询该用户所拥有的权限列表
     *
     * @return
     */
    public List<String> getAuthoritiesName(String adminId) {
        String sql = ""
                + "select a.value"
                + "  from mall_admin u"
                + "  left outer join mall_admin_role ru"
                + "    on u.id = ru.adminset_id"
                + "  left outer join mall_role r"
                + "    on ru.roleset_id = r.id"
                + "  left outer join mall_role_resource ra"
                + "    on r.id = ra.roleset_id"
                + "  left outer join mall_resource a"
                + "    on ra.resourceset_id = a.id"
                + " where u.id = ?";
        return Db.query(sql, adminId);
    }

    /**
     * L根据用户名判断用户收否存在
     */
    public boolean isExistAdmin(String username) {
        return dao.findFirst("select * from mall_admin where username = ?", username) == null ? false : true;
    }

    /**
     * L根据用户名获取系统管理员
     */
    public Admin getSysAdmin(String username, String password) {
        return dao.findFirst("select * from mall_admin where username = ? and password = ?", username, DigestUtils.md5Hex(password));
    }

    /**
     *
     * @param roleId 角色id
     * @return
     */
   public boolean isRelationRoleId(String roleId){
        Admin admin= dao.findFirst("select * from mall_admin where roleId=?",roleId);
        if(admin!=null){
            return true;
        }else{
            return false;
        }

    }
}
